package org.hnxxxy.ebms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.hnxxxy.ebms.common.Result;
import org.hnxxxy.ebms.mapper.RecruitStuMapper;
import org.hnxxxy.ebms.pojo.RecruitStu;
import org.hnxxxy.ebms.pojo.Role;
import org.hnxxxy.ebms.mapper.RoleMapper;
import org.hnxxxy.ebms.pojo.RolePermission;
import org.hnxxxy.ebms.service.RolePermissionService;
import org.hnxxxy.ebms.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hnxxxy.ebms.util.CommonUntil;
import org.hnxxxy.ebms.util.JWTUtil;
import org.hnxxxy.ebms.vo.LoginVo;
import org.hnxxxy.ebms.vo.RoleInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WANGWEI
 * @since 2021-06-10
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RecruitStuMapper recruitStuMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Override
    public Result<String> login(LoginVo loginVo) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_username",loginVo.getUsername());
        Role role = this.baseMapper.selectOne(queryWrapper);
        if (role != null){
            String roleSalt = role.getRoleSalt();
            if (role.getRolePassword().equals(CommonUntil.bCrypt(loginVo.getPassword(),roleSalt))){
                return Result.success(200,"登录成功",JWTUtil.createToken(role.getRoleId(),role.getIsTeacher()));
            }
            return Result.fail("用户名或者密码错误");
        }
        return Result.fail("用户不存在");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<String> register(RecruitStu recruitStu) {
        // 判断当前手机号码是否被注册
        QueryWrapper<RecruitStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recruit_stu_phone",recruitStu.getRecruitStuPhone());
        RecruitStu stu = recruitStuMapper.selectOne(queryWrapper);
        if (stu != null){
            return Result.fail("该手机号码已经被注册");
        }
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Role role = new Role();;
        role.setRoleName(recruitStu.getRecruitStuName());
        role.setRoleUsername(recruitStu.getRecruitStuPhone());
        // 获取salt
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 16; i++) {
            int s = random.nextInt(62);
            stringBuilder.append(str.charAt(s));
        }
        String salt = stringBuilder.toString();
        // 密码加密
        role.setRolePassword(CommonUntil.bCrypt(recruitStu.getRecruitStuPhone(),salt));
        role.setRoleSalt(salt);
        role.setIsTeacher(true);
        recruitStuMapper.insert(recruitStu);
        stu = recruitStuMapper.selectOne(queryWrapper);
        role.setUserId(stu.getRecruitStuId());
        this.baseMapper.insert(role);
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_name",recruitStu.getRecruitStuName())
                .eq("role_username",recruitStu.getRecruitStuPhone());
        Role one = this.getOne(roleQueryWrapper);
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(role.getRoleId());
        rolePermission.setPermissionId(4);
        rolePermissionService.save(rolePermission);
        return Result.success("注册成功");
    }

    @Override
    public Result<RoleInfoVo> info() {
        Integer id = CommonUntil.getId();
        RoleInfoVo roleInfoVo = roleMapper.selectRoleInfo(id);
        return Result.success(roleInfoVo);
    }
}
